"""/* Copyright(C) 2013, OpenSAR by Fan Wang(parai). All rights reserved.
 *
 * This file is part of OpenSAR.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * Email: parai@foxmail.com
 * Source Open At: https://github.com/parai/OpenSAR/
 */
"""
import os

__all__=['Integer','GLInit','GAGet','GASet','GLGet','GHeader','MKDir']

__root=None

def MKDir(p):
    ap = os.path.abspath(p)
    try:
        os.makedirs(ap)
    except:
        if(not os.path.exists(ap)):
            raise Exception('Fatal Error: can\'t create directory <%s>'%(ap))

def Integer(strnum):
    if(strnum.find('0x')!=-1 or strnum.find('0X')!=-1):
        return int(strnum,16)
    else:
        return int(strnum,10)

def GLInit(root):
    global __root
    __root=root

def GAGet(what,which):
    """ Gen Get Attribute"""
    return what.attrib[which]

def GASet(what,which,val):
    """ Gen Set Attribute"""
    what.attrib[which]=val

def GLGet(what,which=None):
    """ Gen Get List
        used to get a sub node list from what if which is None,
        else get which fron what.
    """
    global __root
    if(which == None):
        if(__root.find(what) != None):
            return __root.find(what)
        else:
            return []
    else:
        if(what.find(which) != None):
            return what.find(which)
        else:
            return []
        
def GHeader(ModuleName,Mcu='posix',Vendor='ArcCore'):
    return """/*
* Configuration of module: %s
*
* Created by:   parai
* Copyright:    (C)parai@foxmail.com  
*
* Configured for (MCU):    %s
*
* Module vendor:           %s
* Generator version:       01.00
*
* Generated by easySAR Studio (https://github.com/parai/as)
*/
"""%(ModuleName,Mcu,Vendor)
